Skip to content

tools: lint PR commit messages without approval - #65875

Open
panva wants to merge 1 commit into
nodejs:mainfrom
panva:lint-without-approval
Open

tools: lint PR commit messages without approval#65875
panva wants to merge 1 commit into
nodejs:mainfrom
panva:lint-without-approval

Conversation

@panva

@panva panva commented Sep 7, 2026

Copy link
Copy Markdown
Member

This lets first time contributor PRs know their commit message is invalid without a collaborator having to step in and approve the GHA workflow runs and without checking out the fork code.

Use pull_request_target to run commit message linting without fork workflow approval. Fetch the first commit through the API and pass its message to a pinned validator as JSON on stdin.

Capture validator output and report failures through escaped annotations, using only read access to pull requests.

Use pull_request_target to run commit message linting without fork
workflow approval. Fetch the first commit through the API and pass
its message to a pinned validator as JSON on stdin.

Capture validator output and report failures through escaped
annotations, using only read access to pull requests.

Signed-off-by: Filip Skokan <panva.ip@gmail.com>
@panva
panva requested review from MikeMcC399 and aduh95 September 7, 2026 10:06
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/actions

@panva

panva commented Sep 7, 2026

Copy link
Copy Markdown
Member Author

@nodejs-github-bot nodejs-github-bot added the meta Issues and PRs related to the general management of the project. label Sep 7, 2026

@MikeMcC399 MikeMcC399 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RSLGTM!

I agree very much with the goal of linting the commit message(s) without approval. The new comment text to first-time contributors already reminds about the need for a Signed-off-by trailer, and this PR means they don't have to wait to get checked.

@panva panva added the author ready PRs with CI started, the required approvals, and no outstanding review comments. label Sep 7, 2026
Comment on lines +28 to +52
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const { data: [commit] } = await github.rest.pulls.listCommits({
...context.repo,
pull_number: context.issue.number,
per_page: 1,
});
if (!commit) {
throw new Error('No commits found in pull request');
}
const { exitCode, stdout, stderr } = await exec.getExecOutput('npx', [
'-q', '--yes', '--ignore-scripts', 'core-validate-commit@6.0.0',
'--no-validate-metadata', '--tap', '-',
], {
cwd: process.env.RUNNER_TEMP,
input: Buffer.from(JSON.stringify([{ id: commit.sha, message: commit.commit.message }])),
silent: true,
ignoreReturnCode: true,
});
if (exitCode !== 0) {
core.setFailed(stdout + stderr || 'Commit message validation failed');
} else {
core.info('First commit message passes validation');
}

@aduh95 aduh95 Sep 7, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would work and be more maintainable

Suggested change
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const { data: [commit] } = await github.rest.pulls.listCommits({
...context.repo,
pull_number: context.issue.number,
per_page: 1,
});
if (!commit) {
throw new Error('No commits found in pull request');
}
const { exitCode, stdout, stderr } = await exec.getExecOutput('npx', [
'-q', '--yes', '--ignore-scripts', 'core-validate-commit@6.0.0',
'--no-validate-metadata', '--tap', '-',
], {
cwd: process.env.RUNNER_TEMP,
input: Buffer.from(JSON.stringify([{ id: commit.sha, message: commit.commit.message }])),
silent: true,
ignoreReturnCode: true,
});
if (exitCode !== 0) {
core.setFailed(stdout + stderr || 'Commit message validation failed');
} else {
core.info('First commit message passes validation');
}
run: echo "$COMMITS" | npx -q core-validate-commit -
env:
COMMITS: ${{ toJSON([github.event.commits[0]]) }}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't work. github.event.commits is an integer count for pull_request_target, not the individual commits, and array index literals don't work in expressions either. I also wanted to avoid streaming linter output to avoid workflow-command injection since we're on pull_request_target.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. meta Issues and PRs related to the general management of the project.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants